package fic.storefinder.client.content.composite;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import fic.storefinder.client.content.ContentAbstract;
import fic.storefinder.client.content.ContentContainer;
import fic.storefinder.client.service.LoginService;
import fic.storefinder.client.service.LoginServiceAsync;
import fic.storefinder.shared.utils.Constants;

public class LoginComposite extends ContentAbstract {
	private TextBox textBoxUsername;
	private PasswordTextBox passwordTextBox;
	
	private final LoginServiceAsync loginService = GWT.create(LoginService.class);
	
	public LoginComposite() {

		VerticalPanel verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);

		FlexTable flexTable = new FlexTable();
		verticalPanel.add(flexTable);
		flexTable.setWidth("345px");

		Label lblUsername = new Label("Felhasználónév:");
		flexTable.setWidget(0, 0, lblUsername);

		textBoxUsername = new TextBox();
		flexTable.setWidget(0, 1, textBoxUsername);
		textBoxUsername.setWidth("100%");

		Label lblPassword = new Label("Jelszó:");
		flexTable.setWidget(1, 0, lblPassword);
		
		passwordTextBox = new PasswordTextBox();
		flexTable.setWidget(1, 1, passwordTextBox);
		passwordTextBox.setSize("100%", "34");
		
		
		Button btnSignIn = new Button("Bejelentkezés");
		btnSignIn.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (textBoxUsername.getText().length() == 0 || passwordTextBox.getText().length() == 0) {
					Window.alert("Minden mező kitöltése kötelező!");
				} else {
					
					loginUser();
				}
			}
		});
		flexTable.setWidget(3, 1, btnSignIn);
		/*
		textBoxUsername.setText("fic");
		passwordTextBox.setText("ficpw");
		this.loginUser();
		*/
		
	}
	
	private void loginUser() {
		loginService.LoginUser(textBoxUsername.getText(), passwordTextBox.getText(), new AsyncCallback<String>() {
			
			@Override
			public void onSuccess(String result) {
				// TODO Auto-generated method stub
				if (Constants.LOGIN_OK.equals(result)) {
					ContentContainer.getInstance().setPageContent(new StoreListComposite());
				} else {
					LoginComposite.this.showDialog("Hiba", "Hibás felhasználónév vagy jelszó!");
					//Window.alert("Hibás felhasználónév vagy jelszó!");
				}
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				LoginComposite.this.showDialog("Szerver oldali hiba", caught.toString());
				//Window.alert("Szerver oldali hiba: " + caught.getMessage());
			}
		});
	}
}
